package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.mmlc.inter.InterFragment;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.N4NoParamsRequestFilter;
import com.mmlc.network.responsebean.PrepareResponseBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/30.
 */
public class DiscountCouponSelectFragment extends InterFragment implements AdapterView.OnItemClickListener {
    @Override
    protected int initResourceID() {
        return R.layout.fragment_discount_coupon;
    }

    PullToRefreshListView list_coupon;
    DiscountCouponAdapter adapter;
    DiscountCouponSelectActivity activity;
    @Override
    protected void initView() {
        list_coupon = ViewUtils.getView(view, R.id.list_coupon);
        adapter = new DiscountCouponAdapter(context, datas);
        list_coupon.setAdapter(adapter);
        list_coupon.setMode(PullToRefreshListView.Mode.DISABLED);
        list_coupon.setOnItemClickListener(this);

        activity = (DiscountCouponSelectActivity) getActivity();

    }

    List<PrepareResponseBean.DataBean.CouponListBean>  datas = new ArrayList();

    @Override
    protected void initInfo() {
        super.initInfo();

        final N4NoParamsRequestFilter filter = new N4NoParamsRequestFilter((BaseParentActivity) getActivity(), "prepare");
        if(activity.orderType == 2){//推荐订单
            filter.requestBean.order_id = activity.orderId;
        }

        filter.offerErrorParams((ViewGroup) view);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<PrepareResponseBean>() {
            @Override
            public void onSuccess(PrepareResponseBean result) {
                datas = result.getData().getCoupon_list();
                datas.addAll(result.getData().getCoupon_list());
                if (datas != null && datas.size() > 0)
                    adapter.notifyDataSetChanged();
                else
                    filter.AddNoDataUI();
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.putExtra("id", datas.get(position - 1).getId());
        intent.putExtra("price", datas.get(position - 1).getMoney());
        getActivity().setResult(200, intent);
        getActivity().finish();
    }
}
